`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    12:18:00 04/30/2014 
// Design Name: 
// Module Name:    shootSound 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module shootSound(audioOUT, pwmCLK, soundCLK, enable);


	input enable;      //  begin making sound 
	input pwmCLK;   	 //  PWM carrier frequency
	input soundCLK;    //  beat frequency
	output audioOUT;	 //  output signal to speaker

// internal control signals
	reg [5:0] tone;
//	reg [2:0] dutyCycle; 
	reg [13:0] counter, origin;
	reg [7:0] beat;
	
	reg spkr;
	
	initial begin   // initialize all values to zero
		tone <= 6'd0;
//		dutyCycle <= 3'd0;
		counter <= 14'd0;
		origin <= 14'd0;
		beat <= 8'd0;
		spkr <= 1'b0;
	end

// send speaker signal to output	
	assign audioOUT = (origin != 0)? spkr : 1'b1;



// toggle speaker signal:
	always @(posedge pwmCLK) begin
	  if(counter == 16383) begin //16383 = 11111111111111
		 counter <= origin;
			 spkr <= ~spkr;
		end
		
	  else begin
		 counter <= counter + 14'd1;
	  end
	end


// calculate tones
always @(posedge soundCLK) begin
  case(tone)
    'd0 : origin = 'd0;
    'd1 : origin = 'd4916; //low1 (middle C)
	 'd2 : origin = 'd5560; //low1#
    'd3 : origin = 'd6168; //low2
	 'd4 : origin = 'd6740; //low2#
    'd5 : origin = 'd7281; //low3
    'd6 : origin = 'd7791; //low4
	 'd7 : origin = 'd8275; //low4#
    'd8 : origin = 'd8730; //low5
	 'd9 : origin = 'd9159; //low5#
    'd10 : origin = 'd9565; //low6
	 'd11 : origin = 'd9948; //low6#
    'd12 : origin = 'd10310; //low7
    'd13 : origin = 'd10647; //middle1
	 'd14 : origin = 'd10972; //middle1#
    'd15 : origin = 'd11272; //middle2
	 'd16 : origin = 'd11562; //middle2#
    'd17 : origin = 'd11831; //middle3
    'd18 : origin = 'd12087; //middle4
	 'd19 : origin = 'd12329; //middle4#
    'd20 : origin = 'd12556; //middle5
	 'd21 : origin = 'd12771; //middle5#
    'd22 : origin = 'd12974; //middle6
	 'd23 : origin = 'd13165; //middle6# 
    'd24 : origin = 'd13346; //middle7
    'd25 : origin = 'd13516; //high1
	 'd26 : origin = 'd13677; //high1#	 
    'd27 : origin = 'd13829; //high2
	 'd28 : origin = 'd13972; //high2#
    'd29 : origin = 'd14108; //high3
    'd30 : origin = 'd14235; //high4
	 'd31 : origin = 'd14356; //high4#
    'd32 : origin = 'd14470; //high5
	 'd33 : origin = 'd14577; //high5#
    'd34 : origin = 'd14678; //high6
	 'd35 : origin = 'd14774; //high6#
    'd36 : origin = 'd14864; //high7
    default : origin = 'd0;
  endcase             
end


// write melody here:
always @(posedge soundCLK) begin
	if (enable) begin
			if(beat > 30)  // determines total lenght of sound
				beat = 8'd 31;  // only play through once
			else
				beat = beat + 8'd1;
			
	// run through each beat
		 case(beat)
		  0: tone = 12;
		  1: tone = 13; 
		  2: tone = 12;
		  3: tone = 13;
		  4: tone = 14; 
		  5: tone = 13; 
		  6: tone = 15;
		  7: tone = 14; 
		  8: tone = 16; 
		  9: tone = 15;
		  10: tone = 17;
		  11: tone = 16; 
		  12: tone = 18; 
		  13: tone = 18; 
		  14: tone = 19; 
		  15: tone = 19; 
		  16: tone = 20; 
		  17: tone = 19; 
		  18: tone = 20;
		  19: tone = 19; 
		  20: tone = 20; 
		  default: tone = 0;
		  endcase
		end

	else begin 
			beat = 8'd0;  // reset for a new sound at next enable signal
			tone = 6'd0;
		end
end



endmodule